<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文体会堂预约信息管理系统</title>
    <!-- 引入vue element 使用所需jar包 -->
    <link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../css/indexBackgroundContain.css">
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/element-ui/lib/index.js"></script>
    <link rel="icon" href="../img/favicon.ico" type="image/x-icon"> <!-- 确保 favicon 格式正确 -->
</head>

<body>
<div id="app">
    <div class="header">
        <img src="../img/logo1.png" alt="学校logo" class="logo"> <!-- 左侧logo图片 -->
        <span class="headerPlaceholder"></span>
        <h2 class="textH2">文体会堂预约信息管理系统界面</h2>
    </div>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'loginIndex' }"
                 @click="selectPage('loginIndex')">主界面</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'noticeManage' }"
                 @click="selectPage('noticeManage')">公告管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'placeManage' }"
                 @click="selectPage('placeManage')">场地管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'placeSubscribe' }"
                 @click="selectPage('placeSubscribe')">预约场地</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'alreadySubscribe' }"
                 @click="selectPage('alreadySubscribe')">已预约信息</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'subscribeHistory' }"
                 @click="selectPage('subscribeHistory')">预约历史</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'usersManage' }"
                 @click="selectPage('usersManage')">用户信息管理</div>
            <div class="sidebar-item" :class="{ 'active-module': currentPage === 'myInfo' }"
                 @click="selectPage('myInfo')">我的信息</div>
        </div>

        <div class="main-content">
            <div>
                <el-form :inline="true" :model="selectInfo" class="demo-form-inline">
                    <el-form-item label="场地类别">
                        <el-select v-model="selectInfo.type" placeholder="请选择场地类别" size="medium">
                            <el-option label="室内乒乓球" value="室内乒乓球场"></el-option>
                            <el-option label="室内羽毛球" value="室内羽毛球场"></el-option>
                            <el-option label="室外乒乓球" value="室外乒乓球场"></el-option>
                            <el-option label="室外篮球" value="室外篮球场"></el-option>
                            <el-option label="全部" value="全部"></el-option>
                        </el-select>
                        <el-button type="primary" plain @click="onSelectSubmit">筛选</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <!-- 表格数据 -->
            <template>
                <el-table :data="tableData" height="700" border style="width: 100%">
                    <el-table-column prop="name" label="场地类别" align="center"></el-table-column>
                    <el-table-column prop="site" label="地点" align="center"></el-table-column>
                    <el-table-column prop="facility" label="体育设施" align="center"></el-table-column>
                    <el-table-column prop="description" label="场地描述" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleSubscribe(scope.$index, scope.row)">预约</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </template>


            <!-- 弹出预约对话框 -->
            <el-dialog
                    title="请选择预约时间"
                    :visible.sync="dialogVisibleAdd"
                    width="30%">
                <el-form ref="form" :model="subscribeData" label-width="80px">
                    <el-form-item label="地点">
                        <el-input v-model="subscribeData.place.name" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="地点">
                        <el-input v-model="subscribeData.place.site" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="日期">
                        <el-col :span="11">
                            <el-date-picker type="date" placeholder="选择日期" v-model="subscribeData.dateTime" style="width: 100%;"></el-date-picker>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="时间段">
                        <el-select v-model="subscribeData.timeRange" placeholder="请选择时间段">
                            <!-- 遍历数组生成选项 -->
                            <el-option v-for="item in subscribeData.options" :key="item.value" :label="item.label" :value="item.value" ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="addHandle">提交</el-button>
                        <el-button  @click="dialogVisibleAdd = false">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>

        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: '',

                //查询出来的表格数据，用于切换到当前页面时显示到主容器中
                tableData:[],


                //编辑框中的数据,英文名字具有误导性
                subscribeData:{
                    timeId:-1,
                    dateTime:"",
                    timeRange: "",
                    remark:"",
                    options :[],
                    user: {},
                    place: {}
                },

                history:{
                    historyId :"",
                    user :{},
                    place :{},
                    avaliableTime : {},
                    createTime:"",
                    status: "",
                    remark :""
                },



                //搜索框的默认条件
                selectInfo:{
                    type:""
                },

                // 预约数据对话框是否展示的标记
                dialogVisibleAdd: false,

            };
        },
        methods: {

            selectPage(page) {
                this.currentPage = page; // 设置当前页面标记
                localStorage.setItem('currentPage', page); // 保存当前模块到 localStorage
                const nextUrl = "/pages/" + page + ".html";

                window.location.href = nextUrl; // 页面跳转
            },

            //获取数据
            getTableData(){
                const url = "/placeSubscribes";
                axios.get(url).then((resp)=>{
                    if(resp.data.code == 20001){
                        this.tableData = resp.data.data;
                    }
                })
            },

            //表格中的预约按钮
            handleSubscribe(index, row) {
                this.dialogVisibleAdd = true;
                const url = "/placeSubscribes/byId/"+row.placeId;
                axios.get(url).then((resp)=>{
                    this.getTimeRange();
                    this.subscribeData.user.userId = localStorage.getItem('userId');
                    this.subscribeData.place =  resp.data.data;
                    this.dialogVisibleAdd = true;
                })
            },

            //获取可选择的时间段
            getTimeRange(){
                const url = "/placeSubscribes/getTimeRange";
                axios.get(url).then((resp)=>{
                    this.subscribeData.options = resp.data.data;
                })
            },

            //预约成功提示信息
            editSuccessMsg() {
                this.$message({
                    message: '预约成功',
                    type: 'success'
                })
            },


            //通过状态查询条件信息
            onSelectSubmit(){
                const url = "/placeSubscribes/byType/"+this.selectInfo.type;
                if(this.selectInfo.type != "全部"){
                    axios.get(url).then((resp)=>{
                        if(resp.data.code == 20001){
                            this.tableData = resp.data.data;
                        }
                    })
                }else{
                    this.getTableData();
                }
            },

            //预约提交数据
            addHandle(){
                this.dialogVisibleAdd = true;
                //将数据打包
                this.history.place = this.subscribeData.place;

                const url = "/placeSubscribes";
                axios.post(url,this.place).then((resp)=>{
                    if(resp.data.code == 20051){
                        this.addSuccessMsg();
                    }
                }).finally(() => {
                    this.dialogVisibleAdd = false;
                    this.getTableData();
                })
            },

            //修改成功提示信息
            addSuccessMsg() {
                this.$message({
                    message: '新增成功',
                    type: 'success'
                })
            }
        },


        mounted() {
            const savedPage = localStorage.getItem('currentPage');
            if (savedPage) {
                this.currentPage = savedPage; // 恢复选中的模块
            } else {
                this.currentPage = ''; // 默认模块
            }
            this.getTableData(); // 获取表格数据（保持原逻辑）
        }
    });
</script>
</body>
</html>